package org.lanqiao.dao;

import org.apache.ibatis.annotations.Param;
import org.lanqiao.entity.User;
import org.lanqiao.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDao {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    RedisTemplate redisTemplate;


    public User selectByName(String userName) {
        User user = (User) redisTemplate.opsForValue().get(userName);
        //如果redis里面没有这个user
        if (user == null) {
            //就先去数据库里查询
            user = userMapper.selectByName(userName);
            //放到redis
            redisTemplate.opsForValue().set(userName, user);

            return user;
        } else {
            //有 就直接在redis查询
            return user;
        }

    }

    /**
     * @return org.lanqiao.entity.User
     * @Author 王宇
     * @Description //TODO 通过邮箱密码查询用户(用于登录)
     * @Date 2019/05/23 9:39
     * @Param [userEmail, userPassword]
     */
    public User getUser(String userEmail, String userPassword) {
        User user = userMapper.getUser(userEmail, userPassword);
        return user;
    }

    /**
     * @return java.lang.String
     * @Author 李玉鑫
     * @Description //TODO
     * @Date 2019/5/22 15:08
     * @Param增加用户
     */
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    /**
     * @return java.lang.String
     * @Author 李静
     * @Description //TODO
     * @Date 2019/5/23 10:42
     * @Param 查询全部
     */

    public List<User> queryUserList() {
        List<User> userList = userMapper.queryUserList();
        return userList;
    }


    /**
     * @return void
     * @Author 徐金星
     * @Description //TODO
     * @Date 2019/5/23 10:28
     * @Param [id]
     * @explain 封号解封
     */
    public void updateUserStatusF(String id) {
        userMapper.updateUserStatusF(id);
    }

    public void updateUserStatusJ(String id) {
        userMapper.updateUserStatusJ(id);
    }

    /**
     * 任春旭：查询用户个人信息
     */
    public User queryUser(String userEmail) {
        User user = userMapper.queryUser(userEmail);
        return user;
    }

    /**
     * 任春旭：修改用户昵称
     */
    public User upName(String userName, String userEmail) {
        userMapper.upName(userName, userEmail);
//        修改之后对页面进行刷新获取用户的最新数据
        return userMapper.queryUser(userEmail);
    }

    /**
     * 任春旭：修改用户邮箱
     */
    public User upEmail(String userEmail, String userId) {
        userMapper.upEmail(userEmail, userId);
        return userMapper.queryUser(userEmail);
    }

    /**
     * 任春旭：修改用户密码
     */
    public User upPassword(String userPassword, String userEmail) {
        userMapper.upPassword(userPassword, userEmail);
        return userMapper.queryUser(userEmail);
    }

    /**
     * @return
     * @Author 李玉鑫
     * @Description //TODO
     * @Date 修改用户金额 17:56
     * @Param
     */
    public void updateNumber(String userId,Double userBalance) {
        userMapper.updateNumber(userId,userBalance);
    }
    /**
     * @Author 李玉鑫
     * @Description //TODO
     * @Date id查询个人用户 16:17
     * @Param
     * @return
     */
    public User getUser(String userId){
        return userMapper.getUserById(userId);
    }
}
